﻿using System;
using System.Windows.Controls;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WindowsPhoneMVC.Commands;

namespace WindowsPhoneMVC.Tests.Commands
{
    [TestClass]
    public class NavigateTests
    {
        [TestMethod]
        public void navigate_on_selection_allows_garbage_collection_after_viewmodel_cleanup()
        {
            // arrange 
            var selector = Selector();
            Navigate.SetOnItemSelected((ListBox)selector.Target, "Controller.Action");

            // act
            ((ViewModelBase)((ListBox)selector.Target).DataContext).OnDeactivated();
            ((ListBox) selector.Target).DataContext = null;
            GC.Collect();
            GC.Collect();
            GC.WaitForPendingFinalizers();

            // assert
            Assert.IsFalse(selector.IsAlive);
        }

        private static WeakReference Selector()
        {
            return new WeakReference(new ListBox { DataContext = new ViewModelBase() });
        }
    }
}